﻿using EFModel;
using IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Y2.Filters;

namespace Y2.Controllers
{
    public class usersController : Controller
    {
        private readonly ILogger<usersController> _logger;
        private readonly MyDBContext hs;
        private readonly Iusers he;

        public usersController(ILogger<usersController> logger, Iusers he, MyDBContext hs)
        {
            _logger = logger;
            this.he = he;
            this.hs = hs;
        }
        [TypeFilter(typeof(TestActionFilter))]
        public IActionResult users()
        {
            return View();
        }
        public IActionResult usersol()
        {
            return View();
        }
        //登录查询
        public async Task<IActionResult> UseAll(string name, string pwd)
        {
            var id = hs.users.Where(e => e.u_name == name && e.u_password == pwd).Select(s=> new { s.u_id,s.u_true_name,s.Role.Rolename}).ToList();
            HttpContext.Session.SetString("id", id[0].u_id+"");
            HttpContext.Session.SetString("name", id[0].u_true_name + "");
            HttpContext.Session.SetString("zhiwu", id[0].Rolename + "");
            return Content(JsonConvert.SerializeObject(await he.UseAll(name, pwd)));
        }
        //当前登录人查询
        public async Task<string> Fill(int id)
        {
            return await he.Fill(id);
        }
        //用户查询
        public Task<string> que()
        {
            return he.que();
        }
    }
}
